package net.projectRPG.src;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.renderer.Camera;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import com.jme3.scene.Node;
import com.jme3.export.binary.BinaryImporter;
import com.jme3.export.binary.BinaryExporter;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.logging.Level;

import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.InputCapsule;
import com.jme3.export.OutputCapsule;
import com.jme3.math.Vector3f;

public class MenuScreen implements ScreenController {
    private Nifty nifty;
    private Screen screen;
    private Main main;

    public MenuScreen(Main app){
        main = app;
    }
    
    // When the menu is set up, this is called.
    // Create the local nifty and screen parameters.
    public void bind(Nifty niftyParam, Screen screenParam) {
        nifty = niftyParam;
        screen = screenParam;
    }
    public void onStartScreen() { 
    }
    
    public void onEndScreen() {  
    }
    
    // Resume game
    public void resume(){
        main.getInputManager().setCursorVisible(false);
        main.bulletAppState.setEnabled(true);        
        nifty.exit();
    }      
      
    // Save Game
    public void saveGame(){
        Player player = main.player;
        Node rootNode = main.getRootNode();        
        String userHome = System.getProperty("user.home");
        BinaryExporter exporter = BinaryExporter.getInstance();
        File file = new File(userHome+"/projectRPGSave.j3o");
        try {
          //exporter.save(rootNode, file);
          exporter.save(player, file);
         //exporter.save(main.currentArea, file);
        } catch (IOException ex) {
          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to save node!", ex);
        }      
        
        main.getInputManager().setCursorVisible(false);
        main.bulletAppState.setEnabled(true);        
        nifty.exit();
    }      
      
    // Load Game
    public void loadGame(){
        
        // get objects to load
        Node rootNode = main.getRootNode();    
      
        // cleanup 
        rootNode.detachAllChildren();
        rootNode.getLocalLightList().clear();
        rootNode.getWorldLightList().clear();  
        
        main.currentArea = new Area(main, Area.AreaName.beach);  
        
        // load
        String userHome = System.getProperty("user.home");
        BinaryImporter importer = BinaryImporter.getInstance();
        importer.setAssetManager(main.getAssetManager());
        File file = new File(userHome+"/projectRPGSave.j3o");
        try {
          //Node loadedNode = (Node)importer.load(file);
          //loadedNode.setName("loaded node");
          //rootNode.attachChild(loadedNode);
          main.player = (Player)importer.load(file);
         // main.currentArea = (Area) importer.load(file);
        } catch (IOException ex) {
          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "No saved node loaded.", ex);
        }        
        
        main.getInputManager().setCursorVisible(false);
        main.bulletAppState.setEnabled(true);        
        nifty.exit();
    }      
    
    // New Game
    public void newGame(){
        Node rootNode = main.getRootNode();
        rootNode.detachAllChildren();
        main.areaProcessor.cleanup();
        rootNode.getLocalLightList().clear();
        rootNode.getWorldLightList().clear();
        main.currentArea = new Area(main, Area.AreaName.beach);   
        main.getInputManager().setCursorVisible(false);
        main.bulletAppState.setEnabled(true);        
        nifty.exit();
    }      
    
    // Quit the game.
    public void quit(){
        System.exit(0); 
    }    
    
    public void write(JmeExporter ex) throws IOException {


    }

    public void read(JmeImporter im) throws IOException {

    }    
}
